#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

#endregion

public partial class photo : BlogEngine.Core.Web.Controls.BlogBasePage
{
	/// <summary>
	/// Method to delete photo
	/// </summary>
	protected  void Delete(Guid guid)
	{
		Photo photo = BlogEngine.Core.Photo.GetById(guid);
		photo.Delete();
		photo.Save();
	}

	/// <summary>
	/// Assignes the selected theme to the pages.
	/// </summary>
	protected override void OnPreInit(EventArgs e)
	{
		base.OnPreInit(e);

		if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.QueryString["delete"]))
		{
			if (Page.User.Identity.IsAuthenticated)
			{
				Photo photo = BlogEngine.Core.Photo.GetById(new Guid(Request.QueryString["delete"]));
				if (Page.User.IsInRole(BlogSettings.Instance.AdministratorRole))
                {
                    Delete(photo.Id);
                    Response.Redirect("~/");
                }
			}
		}
	}

	protected void Page_Init(object sender, EventArgs e)
	{
		if (!Page.IsPostBack && !Page.IsCallback)
		{
			if (Request.RawUrl.Contains("?id="))
			{
				Guid id = new Guid(Request.QueryString["id"]);
				Photo photo = Photo.GetById(id);
				if (photo != null)
				{
					Response.Clear();
					Response.StatusCode = 301;
					Response.AppendHeader("location", photo.RelativeLink.ToString());
					Response.End();
				}
			}
		}

		if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
		{
			Guid id = new Guid(Request.QueryString["id"]);
			this.Photo = Photo.GetById(id);

			if (Photo != null)
			{
				if (!this.Photo.IsVisible && !Page.User.Identity.IsAuthenticated)
					Response.Redirect(Utils.RelativeWebRoot + "error404.aspx", true);

				string path = "~/themes/" + BlogSettings.Instance.Theme + "/PhotoView.ascx";

				PhotoView photoView = (PhotoView)LoadControl(path);
				photoView.Photo = Photo;
				photoView.Location = ServingLocation.SinglePost;

				pwPhoto.Controls.Add(photoView);
				//related.Photo = this.Photo; 
				CommentView1.CommentParent = Photo; 

				Page.Title = Photo.Title;
				AddMetaKeywords();
				AddMetaDescription();
				AddGenericLink("last", Photo.Last.Title, Photo.Last.RelativeLink.ToString());
				AddGenericLink("first", Photo.First.Title, Photo.First.RelativeLink.ToString());

				if (Photo.Next != null)
					base.AddGenericLink("next", Photo.Next.Title, Photo.Next.RelativeLink.ToString());

				if (Photo.Previous != null)
					base.AddGenericLink("prev", Photo.Previous.Title, Photo.Previous.RelativeLink.ToString());

				Response.AppendHeader("x-pingback", "http://" + Request.Url.Authority + Utils.RelativeWebRoot + "pingback.axd");
			}
		}
	}

	/// <summary>
	/// Adds the post's description as the description metatag.
	/// </summary>
	private void AddMetaDescription()
	{
		if (!string.IsNullOrEmpty(Photo.Description))
			base.AddMetaTag("description", Photo.Description);
		else
			base.AddMetaTag("description", BlogSettings.Instance.Description);
	}

	/// <summary>
	/// Adds the photo's tags as meta keywords.
	/// </summary>
	private void AddMetaKeywords()
	{
		if (Photo.Tags.Count > 0)
		{
			string[] tags = new string[Photo.Tags.Count];
			for (int i = 0; i < Photo.Tags.Count; i++)
			{
				tags[i] = Photo.Tags[i];
			}
			base.AddMetaTag("keywords", string.Join(",", tags));
		}
	}

	public Photo Photo;
}
